package paziente;

import classi.utente;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *  Visualizzazione pazienti richiamati da una certa data in poi
 * 
 * @author sestari
 */
@WebServlet(name = "listaPaziente", urlPatterns = {"/listaPaziente"})
public class listaPaziente extends HttpServlet {

    /**
     * Crea la pagina HTML dei  pazienti richiamati da una certa data in poi
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            try {

                String data = request.getParameter("data");
                String ora = request.getParameter("ora");
                SimpleDateFormat temp = new SimpleDateFormat("dd/MM/yyyy");
                Date d = new java.sql.Date(temp.parse(data).getTime());
                String dataora = d.toString() + " " + ora;


                ServletContext sc = getServletContext();
                Connection con = (Connection) sc.getAttribute("con");

                HttpSession session = request.getSession();
                utente user = (utente) session.getAttribute("user");



                String comand = "SELECT nome, cognome, data_ultimo_vaccino,nome_medico, cognome_medico,id_paziente "
                        + "FROM `db_user15`.`ultimo_vaccino` where data_ultimo_vaccino <=\"" + dataora + "\"";

                Statement st = (Statement) con.createStatement();
                ResultSet rs = (ResultSet) st.executeQuery(comand);

                out.println("<!DOCTYPE html>  ");
                out.println("<html> ");
                out.println("    <head> ");
                out.println("        <title>Pazienti</title> ");
                out.println("    </head> ");
                out.println("    <body> ");


                out.println("<table border=\"1\"");
                out.println("<tr bgcolor=\"#DEDFF7\">");

                out.println("<td>");
                out.println("Nome");
                out.println("</td>");

                out.println("<td>");
                out.println("Cognome");
                out.println("</td>");

                out.println("<td>");
                out.println("Data Ultimo Vaccino");
                out.println("</td>");

                out.println("<td>");
                out.println("Ora Ultimo Vaccino");
                out.println("</td>");

                out.println("<td>");
                out.println("Medico");
                out.println("</td>");



                out.println("</tr>");





                boolean c = false;
                while (rs.next()) {

                    if (c) {
                        out.println("<tr bgcolor=\"#E6E0E0\">");
                        c = false;
                    } else {
                        out.println("<tr bgcolor=\"white\">");

                        c = true;
                    }


                    out.println("<td>");
                    out.println(rs.getString(1));
                    out.println("</td>");

                    out.println("<td>");
                    out.println(rs.getString(2));
                    out.println("</td>");

                    out.println("<td>");
                    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                    out.println(dateFormat.format(rs.getDate(3)));
                    out.println("</td>");

                    out.println("<td>");
                    dateFormat = new SimpleDateFormat("HH:mm:ss");
                    out.println(dateFormat.format(rs.getTime(3)));
                    out.println("</td>");

                    out.println("<td>");
                    out.println(rs.getString(4) + " " + rs.getString(5));
                    out.println("</td>");





                    out.println("<td>");
                    out.println("<a href=\"profiloUtente?id=" + rs.getString(6) + "\"><IMG SRC=\"images/lupa.gif\"  WIDTH=\"20\" HEIGHT=\"20\" ALT=\"Detagli\"> </a>");
                    out.println("</ td>");

                    out.println("</tr>");

                }

                out.println("<tr bgcolor=\"#DEDFF7\">");
                out.print("                <form action=\"logoff\" method=\"post\"> ");
                out.print("                    <td> ");
                out.print("                        <input type=\"submit\"  value=\"Logoff\"/>                         ");
                out.print("                    </td>                   ");
                out.print("                </form> ");

                out.print("                <form action=\"welcome\" method=\"post\"> ");
                out.print("                    <td> ");
                out.print("                        <input type=\"submit\"  value=\"Tornare\"/>                         ");
                out.print("                    </td>                   ");
                out.print("                </form> ");
                out.println("</tr>");
                out.println("</table>");

                out.println("</body>  ");
                out.println("</html> ");


            } finally {
                out.close();
            }
        } catch (Exception e) {
        }
    }

    /**
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
